﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using TimeLineDataContract;

namespace TimeLineControlPowerPointAddIn
{
    public partial class FrmDataTimeLineControl : Form
    {
        private readonly SlideDeskContract _dataSlide;

        public FrmDataTimeLineControl(SlideDeskContract dataslide,string[] sugestTitle)
        {
            _dataSlide = dataslide;
            InitializeComponent();
            CboTitle.Text  = _dataSlide.Title;
            CboTitle.Items.AddRange(sugestTitle);
            MaskTime.Text = _dataSlide.ElaspTime.ToString("c").Replace(":", "");
            ChkFullScreen.Checked = _dataSlide.FullScreen;
            if (_dataSlide.DockPosition == DockWindows.Top)
            {
                OptTop.Checked = true;
            }
            if (_dataSlide.DockPosition == DockWindows.Bottom)
            {
                OptBottom.Checked = true;
            }

            var tm = TimeSpan.Parse(MaskTime.Text);
            trackHor.Value = tm.Hours;
            trackMin.Value = tm.Minutes;
            trackSec.Value = tm.Seconds;
        }

        private void FrmDataTimeLineControl_Load(object sender, EventArgs e)
        {
            MaskTime.Tag = MaskTime.Text;
            CboTitle.Tag = CboTitle.Text;
        }

        private void TrackSecValueChanged(object sender, EventArgs e)
        {
            var tm = MaskTime.Text.Split(':');
            tm[0] = tm[0].PadLeft(2, '0');
            tm[1] = tm[1].PadLeft(2, '0');
            tm[2] = tm[2].PadLeft(2, '0');

            tm[2] = trackSec.Value.ToString().PadLeft(2,'0');
            MaskTime.Text = tm[0] + tm[1] + tm[2];
            MaskTime.Tag = MaskTime.Text;
        }

        private void TrackMinValueChanged(object sender, EventArgs e)
        {
            var tm = MaskTime.Text.Split(':');
            tm[0] = tm[0].PadLeft(2, '0');
            tm[1] = tm[1].PadLeft(2, '0');
            tm[2] = tm[2].PadLeft(2, '0');

            tm[1] = trackMin.Value.ToString().PadLeft(2, '0');
            MaskTime.Text = tm[0] + tm[1] + tm[2];
            MaskTime.Tag = MaskTime.Text;
        }

        private void TrackHorValueChanged(object sender, EventArgs e)
        {
            var tm = MaskTime.Text.Split(':');
            tm[0] = tm[0].PadLeft(2, '0');
            tm[1] = tm[1].PadLeft(2, '0');
            tm[2] = tm[2].PadLeft(2, '0');

            tm[0] = trackHor.Value.ToString().PadLeft(2, '0');
            MaskTime.Text = tm[0] + tm[1] + tm[2];
            MaskTime.Tag = MaskTime.Text;
        }

        private void MaskTime_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                TimeSpan.Parse(MaskTime.Text);
                ErrPrvName.SetError(MaskTime,null);
                MaskTime.Tag = MaskTime.Text;
            }
            catch
            {
                 e.Cancel = true;
                ErrPrvName.SetError(MaskTime, ResMng.StringResource("MsgErrSlideTime"));
                MaskTime.Text = MaskTime.Tag.ToString().Replace(":","");
            }
        }

        private void CboTitle_Validating(object sender, CancelEventArgs e)
        {
            ErrPrvName.SetError(MaskTime, null);
            CboTitle.Text = CboTitle.Text.Trim();
            if (CboTitle.Text.Length == 0)
            {
                e.Cancel = true;
                ErrPrvName.SetError(CboTitle, ResMng.StringResource("MsgErrSlideTitle"));
                CboTitle.Text = CboTitle.Tag.ToString();
                return;
            }
            CboTitle.Tag = CboTitle.Text;
        }

        private void BnrCancel_Click(object sender, EventArgs e)
        {
            var rib = (RibbonTimeLineControl)Globals.Ribbons.GetRibbon(typeof(RibbonTimeLineControl));
            rib.DataWizardResult = null;
            Close();
        }

        private void BntOk_Click(object sender, EventArgs e)
        {
            var rib = (RibbonTimeLineControl)Globals.Ribbons.GetRibbon(typeof(RibbonTimeLineControl));
            rib.DataWizardResult = new SlideDeskContract();
            if (OptTop.Checked)
                rib.DataWizardResult.DockPosition = DockWindows.Top;
            if (OptBottom.Checked)
                rib.DataWizardResult.DockPosition = DockWindows.Bottom;
            rib.DataWizardResult.ElaspTime = TimeSpan.Parse(MaskTime.Text);
            rib.DataWizardResult.FullScreen = ChkFullScreen.Checked;
            rib.DataWizardResult.Id = _dataSlide.Id;
            rib.DataWizardResult.SlideNumber = _dataSlide.SlideNumber;
            rib.DataWizardResult.Title = CboTitle.Text;
            Close();
        }

        private void MaskTime_Validated(object sender, EventArgs e)
        {
            var tm = TimeSpan.Parse(MaskTime.Text);
            trackHor.Value = tm.Hours;
            trackMin.Value = tm.Minutes;
            trackSec.Value = tm.Seconds;
        }
    }
}
